package com.shanwen.shopping.utils;

import android.content.Context;
import android.util.Log;

import com.shanwen.greendao.gen.GoodsInfoDao;
import com.shanwen.shopping.entity.GoodsInfo;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lex on 2017/10/9.
 */

public class GoodsDaoUtil {
    private static final String TAG = GoodsDaoUtil.class.getSimpleName();
    private DaoManager mManager;

    public GoodsDaoUtil(Context context){
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 插入，如果表未创建，先创建表
     * @return
     */
    public boolean insertGoodsInfo(GoodsInfo goodsInfo){
        boolean flag = false;
        flag = mManager.getDaoSession().getGoodsInfoDao().insertOrReplace(goodsInfo) == -1 ? false : true;
        Log.i(TAG, "insert GoodsInfo :" + flag + "-->" + goodsInfo.toString());
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     * @return
     */
    public boolean insertMultGoodsInfo(final List<GoodsInfo> goodsInfoList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (GoodsInfo goodsInfo : goodsInfoList) {
                        mManager.getDaoSession().insertOrReplace(goodsInfo);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     * @return
     */
    public boolean updateGoodsInfo(GoodsInfo goodsInfo){
        boolean flag = false;
        try {
            mManager.getDaoSession().update(goodsInfo);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     * @return
     */
    public boolean deleteGoodsInfo(GoodsInfo goodsInfo){
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(goodsInfo);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     * @return
     */
    public boolean deleteAll(){
        boolean flag = false;
        try {
            mManager.getDaoSession().deleteAll(GoodsInfo.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<GoodsInfo> queryAllGoodsInfo(){
        return mManager.getDaoSession().loadAll(GoodsInfo.class);
    }

    /**
     * 根据主键id查询记录
     * @param id
     * @return
     */
//    public GoodsInfo queryGoodsInfoById(long key){
//        return mManager.getDaoSession().load(GoodsInfo.class, key);
//    }

    public GoodsInfo queryGoodsInfoById(long id){
        QueryBuilder<GoodsInfo> queryBuilder = mManager.getDaoSession().queryBuilder(GoodsInfo.class);
        return queryBuilder.where(GoodsInfoDao.Properties.GoodsId.eq(id)).unique();
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<GoodsInfo> queryGoodsInfoByNativeSql(String sql, String[] conditions){
        return mManager.getDaoSession().queryRaw(GoodsInfo.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行id查询
     * @return
     */
    public List<GoodsInfo> queryGoodsInfoByQueryBuilder(long id){
        QueryBuilder<GoodsInfo> queryBuilder = mManager.getDaoSession().queryBuilder(GoodsInfo.class);
        return queryBuilder.where(GoodsInfoDao.Properties.GoodsId.eq(id)).list();
    }

    /**
     * 使用queryBuilder查询所有记录
     * @return
     */
    public List<GoodsInfo> queryAllGoodsInfoByQueryBuilder(){
        QueryBuilder<GoodsInfo> queryBuilder = mManager.getDaoSession().queryBuilder(GoodsInfo.class);
        return queryBuilder.where(GoodsInfoDao.Properties.Num.notEq(0)).list();
    }

    //更新数据
    public void updateGoodsInfoField(GoodsInfo goodsInfo,int num){
        QueryBuilder<GoodsInfo> queryBuilder = mManager.getDaoSession().queryBuilder(GoodsInfo.class);
        GoodsInfo goodsInfo1 = queryBuilder.where(GoodsInfoDao.Properties.GoodsId.eq(goodsInfo.getGoodsId())).unique();
        if (goodsInfo1!=null){
            goodsInfo1.setNum(num);
            mManager.getDaoSession().update(goodsInfo1);
        }
    }
}
